﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;

namespace foursquare
{
    class Logging
    {

        // This method enables general logging. It contains parameters
        // to specify a path, and Boolean values of true to include
        // the application name, process ID, and events in the log.
        public void EnableLogging(bool useApp, bool usePid, bool useFlush)
        {
            // Specify values for setting the registry.
            string userRoot = "HKEY_LOCAL_MACHINE";
            string subkey = "SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging";
            string keyName = userRoot + "\\" + subkey;

            // Set the Enabled registry value.
            Registry.SetValue(keyName, "Enabled", 1);

            if (useApp == true)
                Registry.SetValue(keyName, "UseApp", 1);
            else
                Registry.SetValue(keyName, "UseApp", 0);

            if (usePid == true)
                Registry.SetValue(keyName, "UsePid", 1);
            else
                Registry.SetValue(keyName, "UsePid", 0);

            if (useFlush == true)
                Registry.SetValue(keyName, "UseFlush", 1);
            else
                Registry.SetValue(keyName, "UseFlush", 0);
        }

        // This method sets the Enabled key value to 1
        // so that logging for Interoperability is enabled.
        public void SetInteropLogging(bool logOn)
        {
            // Specify values for setting the registry.
            string userRoot = "HKEY_LOCAL_MACHINE";
            string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Interop";
            string keyName = userRoot + "\\" + subkey;

            int logSet;
            if (logOn == true)
                logSet = 1;
            else
                logSet = 0;

            // Set the registry value.
            try
            {
                Registry.SetValue(keyName, "Enabled", logSet);
                if (logOn == true)
                    MessageBox.Show("Interop Logging On");
                else
                    MessageBox.Show("Interop Logging Off");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // This method sets the Enabled key value to 1
        // so that logging for class loading is enabled.
        public void SetLoaderLogging(bool logOn)
        {
            // Specify values for setting the registry.
            string userRoot = "HKEY_LOCAL_MACHINE";
            string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Loader";
            string keyName = userRoot + "\\" + subkey;

            int logSet;
            if (logOn == true)
                logSet = 1;
            else
                logSet = 0;

            // Set the registry value.
            try
            {
                Registry.SetValue(keyName, "Enabled", logSet);
                if (logOn == true)
                    MessageBox.Show("Loader Logging On");
                else
                    MessageBox.Show("Loader Logging Off");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // This method sets the Enabled key value to 1
        // so that logging for networking is enabled.
        public void SetNetworkLogging(bool logOn)
        {
            // Specify values for setting the registry.
            string userRoot = "HKEY_LOCAL_MACHINE";
            string subkey = "Software\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Network";
            string keyName = userRoot + "\\" + subkey;

            int logSet;
            if (logOn == true)
                logSet = 1;
            else
                logSet = 0;

            // Set the registry value.
            try
            {
                Registry.SetValue(keyName, "Enabled", logSet);
                if (logOn == true)
                    MessageBox.Show("Networking Logging On");
                else
                    MessageBox.Show("Networking Loggin Off");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // This method disables all logging.
        public void DisableLogging()
        {
            // Specify values for setting the registry.
            string userRoot = "HKEY_LOCAL_MACHINE";
            string subkey = "SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging";
            string keyName = userRoot + "\\" + subkey;

            // Set the Enabled registry value.
            Registry.SetValue(keyName, "Enabled", 0);
            MessageBox.Show("Logging Disabled");
        }

        // This method recursively examines the keys
        // under the Logging subkey and writes their
        // key names and values to a log file. It saves
        // the information in "logsettings.txt" located
        // in the directory that contains this 
        // example application.
        public void WriteLoggingSettings()
        {
            StreamWriter sw = new StreamWriter("logsettings.txt", false);
            sw.WriteLine("General Logging Settings:");
            RegistryKey rkLogging = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging");
            string[] valNames = rkLogging.GetValueNames();
            for (int x = 0; x < valNames.Length; x++)
            {
                sw.WriteLine(valNames[x].ToString() + ": " + rkLogging.GetValue(valNames[x]).ToString());
            }

            sw.WriteLine();
            sw.WriteLine("Interop Logging:");
            RegistryKey rkInterop = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Interop");
            string[] interopNames = rkInterop.GetValueNames();
            for (int x = 0; x < interopNames.Length; x++)
            {
                sw.WriteLine(interopNames[x].ToString() + ": " + rkInterop.GetValue(interopNames[x]).ToString());
            }

            sw.WriteLine();
            sw.WriteLine("Loader Logging:");
            RegistryKey rkLoader = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Loader");
            string[] loaderNames = rkLoader.GetValueNames();
            for (int x = 0; x < loaderNames.Length; x++)
            {
                sw.WriteLine(loaderNames[x].ToString() + ": " + rkLoader.GetValue(loaderNames[x]).ToString());
            }

            sw.WriteLine();
            sw.WriteLine("Networking Logging:");
            RegistryKey rkNetworking = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\.NETCompactFramework\\Diagnostics\\Logging\\Network");
            string[] netNames = rkNetworking.GetValueNames();
            for (int x = 0; x < netNames.Length; x++)
            {
                sw.WriteLine(netNames[x].ToString() + ": " + rkNetworking.GetValue(netNames[x]).ToString());
            }
            sw.Close();
        }
    }
}
